﻿Imports Models.Users
Imports System.ComponentModel.DataAnnotations

Public Class Companies
    Inherits BaseService

    Sub New(request As System.Net.Http.HttpRequestMessage)
        Me.loadToken(request)
    End Sub
    Sub New(token As String)
        Me.token = token
    End Sub

    ''' <summary>
    ''' Gets the specified id.
    ''' </summary>
    ''' <param name="id">The id.</param>
    ''' <returns></returns>
    Public Function [Get](id As Integer) As CompanyVO
        Try
            Me.checkAccess("GETCOMPANY")
            Dim oEntity As CompanyVO

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                Dim oDataEntity As Data.BaseModel.Company = (From u In oDB.Companies Where u.CompanyId = id Select u).FirstOrDefault()

                If oDataEntity IsNot Nothing Then
                    oEntity = New CompanyVO(oDataEntity)
                End If
            End Using

            Return oEntity

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try


    End Function

    ' ''' <summary>
    ' ''' Gets the specified login.
    ' ''' </summary>
    ' ''' <param name="login">The id.</param>
    ' ''' <returns></returns>
    'Public Function [Get](login As String) As UserVO
    '    Try
    '        '' Sin permisos y sin controller. Creado para acceder al User en el startup del proyecto FastPath
    '        Dim oEntity As UserVO

    '        Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
    '            Dim oDataEntity As Data.BaseModel.User = (From u In oDB.Users Where u.UserLogin = login Select u).FirstOrDefault()

    '            If oDataEntity IsNot Nothing Then
    '                oEntity = New UserVO(oDataEntity)
    '            End If
    '        End Using

    '        Return oEntity

    '    Catch ex As Exception
    '        MainLib.Helpers.ExceptionWrapper(ex)
    '    End Try

    'End Function


    ''' <summary>
    ''' Get List of Companies
    ''' </summary>
    ''' <param name="companyId">optional for filter</param>
    ''' <returns>Array of Companies</returns>
    ''' <remarks></remarks>
    Public Function List(Optional companyId As Integer = -1) As IEnumerable(Of CompanyVO)
        Try
            Me.checkAccess("LISTCOMPANIES")
            Dim oResult As New Generic.List(Of CompanyVO)
            Dim oDataEntity As New Generic.List(Of Data.BaseModel.Company)

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                oDataEntity = oDB.Companies.ToList()
                'If Not companyId = -1 Then
                '    Dim oListUserComp As New Generic.List(Of Data.BaseModel.UserCompany)
                '    Dim query As IQueryable(Of Data.BaseModel.UserCompany) = From c In oDB.UserCompanies Where c.CompanyId = companyId Select c
                '    oListUserComp = query.ToList()
                '    Dim oDataAux As New Generic.List(Of Data.BaseModel.User)
                '    oDataAux.AddRange(oDataEntity)
                '    For Each oItemUser As Data.BaseModel.User In oDataEntity
                '        For Each oItemUserComp As Data.BaseModel.UserCompany In oListUserComp
                '            If oItemUser.UserId = oItemUserComp.UserId Then
                '                Exit For
                '            ElseIf oItemUserComp Is oListUserComp.Last Then
                '                '' Sino esta en la lista, lo saca
                '                oDataAux.Remove(oItemUser)
                '            End If
                '        Next
                '    Next
                '    oDataEntity.Clear()
                '    oDataEntity.AddRange(oDataAux)
                'End If

                If oDataEntity IsNot Nothing Then
                    For Each dbCompany As Data.BaseModel.Company In oDataEntity
                        oResult.Add(New CompanyVO(dbCompany))
                    Next
                End If

            End Using

            Return oResult

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try
    End Function

    ''' <summary>
    ''' Get List of Companies
    ''' </summary>
    ''' <param name="companyId">optional for filter</param>
    ''' <returns>Array of Companies with groups, company preferences and rols data</returns>
    ''' <remarks></remarks>
    Public Function ListFull(Optional companyId As Integer = -1) As IEnumerable(Of FullCompanyVO)
        Try
            Me.checkAccess("LISTCOMPANIES")
            Dim oResult As New Generic.List(Of FullCompanyVO)
            Dim oDataEntity As New Generic.List(Of Data.BaseModel.Company)

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")
                oDataEntity = oDB.Companies.ToList()
                'If Not companyId = -1 Then
                '    Dim oListUserComp As New Generic.List(Of Data.BaseModel.UserCompany)
                '    Dim query As IQueryable(Of Data.BaseModel.UserCompany) = From c In oDB.UserCompanies Where c.CompanyId = companyId Select c
                '    oListUserComp = query.ToList()
                '    Dim oDataAux As New Generic.List(Of Data.BaseModel.User)
                '    oDataAux.AddRange(oDataEntity)
                '    For Each oItemUser As Data.BaseModel.User In oDataEntity
                '        For Each oItemUserComp As Data.BaseModel.UserCompany In oListUserComp
                '            If oItemUser.UserId = oItemUserComp.UserId Then
                '                Exit For
                '            ElseIf oItemUserComp Is oListUserComp.Last Then
                '                '' Sino esta en la lista, lo saca
                '                oDataAux.Remove(oItemUser)
                '            End If
                '        Next
                '    Next
                '    oDataEntity.Clear()
                '    oDataEntity.AddRange(oDataAux)
                'End If

                If oDataEntity IsNot Nothing Then
                    For Each dbCompany As Data.BaseModel.Company In oDataEntity
                        oResult.Add(New FullCompanyVO(dbCompany))
                    Next
                End If

            End Using

            Return oResult

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
        End Try
    End Function

    ''' <summary>
    ''' Update a Company
    ''' </summary>
    ''' <param name="company"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Update(company As CompanyVO) As Integer
        Try
            Me.checkAccess("UPDATECOMPANY")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As Data.BaseModel.Company = (From c In oDB.Companies Where c.CompanyId = company.companyId Select c).FirstOrDefault()

                If oData IsNot Nothing Then

                    'If Not String.IsNullOrEmpty(user.fullName) Then
                    '    oDataUser.FullName = user.fullName
                    'End If
                    'If Not String.IsNullOrEmpty(user.email) Then
                    '    oDataUser.Email = user.email
                    'End If
                    'If Not String.IsNullOrEmpty(user.userLogin) Then
                    '    oDataUser.UserLogin = user.userLogin
                    'End If
                    'If Not String.IsNullOrEmpty(user.password) Then
                    '    oDataUser.Password = user.password
                    'End If
                    'If Boolean.TryParse(user.active, True) Then
                    '    oDataUser.Active = user.active
                    'End If

                    nRes = oDB.SaveChanges()

                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try
    End Function

    ''' <summary>
    ''' Create a Company
    ''' </summary>
    ''' <param name="company"></param>
    ''' <returns>The new ID  of the entity</returns>
    ''' <remarks></remarks>
    Public Function Create(company As CompanyVO) As Integer
        Try
            Me.checkAccess("CREATECOMPANY")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As New Data.BaseModel.Company()
                If oData IsNot Nothing Then
                    'If Not String.IsNullOrEmpty(user.fullName) Then
                    '    oDataUser.FullName = user.fullName
                    'End If
                    'If Not String.IsNullOrEmpty(user.email) Then
                    '    oDataUser.Email = user.email
                    'End If
                    'If Not String.IsNullOrEmpty(user.userLogin) Then
                    '    oDataUser.UserLogin = user.userLogin
                    'End If
                    'If Not String.IsNullOrEmpty(user.password) Then
                    '    oDataUser.Password = user.password
                    'End If
                    'If Boolean.TryParse(user.active, True) Then
                    '    oDataUser.Active = user.active
                    'End If

                    oDB.Companies.AddObject(oData)
                End If
                If oDB.SaveChanges() > 0 Then
                    nRes = oData.CompanyId
                End If
            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

    ''' <summary>
    ''' Delete a company
    ''' </summary>
    ''' <param name="id">companyId</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Delete(id As Integer) As Integer
        Try
            Me.checkAccess("DELETECOMPANY")
            Dim nRes As Integer = -1

            Using oDB As New Data.BaseModel.BaseModelContainer("name=BaseModelContainer")

                Dim oData As Data.BaseModel.Company = (From c In oDB.Companies Where c.CompanyId = id Select c).FirstOrDefault()
                
                If oData IsNot Nothing Then
                    'If oListCompanies.Any() Then
                    '    For Each oItem As Data.BaseModel.UserCompany In oListCompanies
                    '        oItem.UserPreference.Clear()
                    '        oItem.Groups.Clear()
                    '        oDB.DeleteObject(oItem)
                    '    Next
                    'End If

                    oDB.DeleteObject(oData)
                    nRes = oDB.SaveChanges()
                End If

            End Using

            Return nRes

        Catch ex As Exception
            MainLib.Helpers.ExceptionWrapper(ex)
            Return Nothing
        End Try

    End Function

End Class